package logic

import (
	"context"
	"month/videosvr/internal/service"
	"strconv"

	"month/videosvr/internal/svc"
	"month/videosvr/video"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentLikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentLikeLogic {
	return &CommentLikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// CommentLike todo 评论点赞
func (l *CommentLikeLogic) CommentLike(in *video.CommentLikeReq) (*video.CommentLikeRes, error) {
	// todo: add your logic here and delete this line
	id, _ := strconv.Atoi(in.CommentId)
	key := in.CommentId + "Like:" + in.Userid
	//todo 判断有没有点赞
	is, _ := service.IsKey(key)
	if is == 0 {
		//todo 没有点赞 添加redis 表示给指定评论点赞
		service.CreateLike(key)
		comment, err := service.GetCommentAll(id)
		if err != nil {
			return &video.CommentLikeRes{}, err
		}
		//todo 同时数据库点赞量+1
		comment.LikeCount += 1
		err = service.SaveComment(comment.LikeCount)
		if err != nil {
			return &video.CommentLikeRes{}, err
		}
		return &video.CommentLikeRes{}, nil
	}
	//todo 如果已经点过赞 删除redis 表示取消点赞
	_, err := service.DelLike(key)
	if err != nil {
		return &video.CommentLikeRes{}, err
	}
	//todo 同时数据库点赞量-1
	comment, err := service.GetCommentAll(id)
	if err != nil {
		return &video.CommentLikeRes{}, err
	}
	comment.LikeCount -= 1
	err = service.SaveComment(comment.LikeCount)
	if err != nil {
		return &video.CommentLikeRes{}, err
	}
	return &video.CommentLikeRes{}, nil
}
